home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
3863
/
3863.xpi
/
chrome
/
imacros.jar
/
content
/
utils.js
< prev
next >
Wrap
Text File
|
2010-01-25
|
12KB
|
1 lines
var imns={get Ci(){return Components.interfaces;},get Cc(){return Components.classes;},get osvc(){var os=this.Cc["@mozilla.org/observer-service;1"];return os.getService(this.Ci.nsIObserverService);},get prefsvc(){var ps=this.Cc["@mozilla.org/preferences-service;1"];return ps.getService(this.Ci.nsIPrefService);},get consvc(){var cs=this.Cc["@mozilla.org/consoleservice;1"];return cs.getService(this.Ci.nsIConsoleService);}};var im_FIO={openNode:function(name){var node=imns.Cc['@mozilla.org/file/local;1'];node=node.createInstance(imns.Ci.nsILocalFile);node.initWithPath(name);return node;},openMacroFile:function(macro){var file=im_Pref.getFilePref("defsavepath");var nodes=macro.split(__psep()).reverse();while(nodes.length)file.append(nodes.pop());return file;},makeDirectory:function(name){var dir=this.openNode(name);if(dir.exists()){if(!dir.isDirectory()){dir.remove(false);dir.create(imns.Ci.nsIFile.DIRECTORY_TYPE,0777);}}else{dir.create(imns.Ci.nsIFile.DIRECTORY_TYPE,0777);}return dir;},copyFiles:function(src,dst){try{if(src==dst){Components.utils.reportError("copyFiles error: src equals dst!");return false;}var src_dir=this.openNode(src);if(!src_dir.exists()){Components.utils.reportError("copyFiles error: src doesn't exists!");return false;}var dst_dir=this.makeDirectory(dst);var entries=src_dir.directoryEntries;while(entries.hasMoreElements()){var tmp=null;var entry=entries.getNext().QueryInterface(imns.Ci.nsILocalFile);if(entry.isDirectory()){tmp=dst_dir.clone();tmp.append(entry.leafName);this.copyFiles(entry.path,tmp.path);}else{tmp=this.openNode(dst_dir.path);tmp.append(entry.leafName);if(tmp.exists())tmp.remove(false);entry.copyTo(dst_dir,null);}}}catch(e){Components.utils.reportError(e);return false;}return true;},_uconv:function(){var uniconv=imns.Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(imns.Ci.nsIScriptableUnicodeConverter);return uniconv;},_ios:function(){var ios=imns.Cc["@mozilla.org/network/io-service;1"].getService(imns.Ci.nsIIOService);return ios;},_fos:function(){var fos=imns.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(imns.Ci.nsIFileOutputStream);return fos;},_fis:function(){var fis=imns.Cc["@mozilla.org/network/file-input-stream;1"].createInstance(imns.Ci.nsIFileInputStream);return fis;},convertToUTF8:function(str){var uniconv=this._uconv();uniconv.charset='UTF-8';return uniconv.ConvertFromUnicode(str);},convertFromUTF8:function(str){var uniconv=this._uconv();uniconv.charset='UTF-8';return uniconv.ConvertToUnicode(str);},_write:function(file,str){var fos=this._fos();fos.init(file,0x02|0x08|0x20,0777,0);fos.write(str,str.length);fos.close();},_append:function(file,str){var fos=imns.Cc["@mozilla.org/network/file-output-stream;1"].createInstance(imns.Ci.nsIFileOutputStream);fos.init(file,0x02|0x08|0x10,0777,0);fos.write(str,str.length);fos.close();},detectBOM:function(file){var ios=this._ios();var bstream=imns.Cc["@mozilla.org/binaryinputstream;1"].getService(imns.Ci.nsIBinaryInputStream);var channel=ios.newChannelFromURI(ios.newFileURI(file));var input=channel.open();bstream.setInputStream(input);var charset="unknown";if(input.available()>4){var data=bstream.readBytes(4);if(data.charCodeAt(0)==239&&data.charCodeAt(1)==187&&data.charCodeAt(2)==191){charset="UTF-8";}else if(data.charCodeAt(0)==0&&data.charCodeAt(1)==0&&data.charCodeAt(2)==254&&data.charCodeAt(3)==255){charset="UTF-32BE";}else if(data.charCodeAt(0)==255&&data.charCodeAt(1)==254&&data.charCodeAt(2)==0&&data.charCodeAt(3)==0){charset="UTF-32LE";}else if(data.charCodeAt(0)==255&&data.charCodeAt(1)==254){charset="UTF-16LE";}else if(data.charCodeAt(0)==254&&data.charCodeAt(1)==255){charset="UTF-16BE";}}bstream.close();input.close();return charset;},_read:function(file,charset){var is=imns.Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(imns.Ci.nsIConverterInputStream);const rc=imns.Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER;var fis=this._fis(),data="",str={};fis.init(file,-1,0,0);is.init(fis,charset,1024,rc);while(is.readString(4096,str)!=0)data+=str.value;is.close();return data;},readTextFile:function(file){var charset=this.detectBOM(file),data="";if(charset!="unknown")return this._read(file,charset);var branch=imns.prefsvc.getBranch("intl.charset.");var defCharset=branch.getCharPref("default");var charsetList=["UTF-8",defCharset,"UTF-16","UTF-32"];for(var i=0;i<charsetList.length;i++){data=this._read(file,charsetList[i]);if(data.match(/\uFFFD|\0/))continue;else return data;}throw new Error("Unable to detect the file ("+file.path+") charset");},writeTextFile:function(file,text){var utf8bom=String.fromCharCode(239)+String.fromCharCode(187)+String.fromCharCode(191);var data=utf8bom+this.convertToUTF8(text);this._write(file,data);},appendTextFile:function(file,text){var data=this.convertToUTF8(text);if(!file.exists()){var utf8bom=String.fromCharCode(239)+String.fromCharCode(187)+String.fromCharCode(191);data=utf8bom+data;}this._append(file,data);},};var im_Dialogs={browseForFolder:function(title,defdir){try{var fp=imns.Cc["@mozilla.org/filepicker;1"].createInstance(imns.Ci.nsIFilePicker);fp.init(window,title,imns.Ci.nsIFilePicker.modeGetFolder);if(defdir)fp.displayDirectory=defdir;var rv=fp.show();if(rv==imns.Ci.nsIFilePicker.returnOK){return fp.file;}}catch(e){Components.utils.reportError(e);}return null;},browseForFileSave:function(title,filename,defdir){try{var fp=imns.Cc["@mozilla.org/filepicker;1"].createInstance(imns.Ci.nsIFilePicker);fp.init(window,title,imns.Ci.nsIFilePicker.modeSave);fp.defaultString=filename;if(/\.js$/.test(filename))fp.appendFilter("iMacros script","*.js");else if(/\.iim$/.test(filename))fp.appendFilter("iMacros macro","*.iim");else if(/\.(?:png|jpe?g)$/.test(filename))fp.appendFilters(fp.filterImages);fp.appendFilters(imns.Ci.nsIFilePicker.filterAll);fp.filterIndex=0;var rootdir=defdir?defdir:im_Pref.getFilePref("defsavepath");fp.displayDirectory=rootdir;var r=fp.show();if(r==imns.Ci.nsIFilePicker.returnOK||r==imns.Ci.nsIFilePicker.returnReplace){return fp.file;}}catch(e){Components.utils.reportError(e);}return null;},browseForFileOpen:function(title,defdir){try{var fp=imns.Cc["@mozilla.org/filepicker;1"].createInstance(imns.Ci.nsIFilePicker);fp.init(window,title,imns.Ci.nsIFilePicker.modeOpen);fp.appendFilters(imns.Ci.nsIFilePicker.filterAll);fp.filterIndex=0;var rootdir=defdir?defdir:im_Pref.getFilePref("defsavepath");fp.displayDirectory=rootdir;var r=fp.show();if(r==imns.Ci.nsIFilePicker.returnOK||r==imns.Ci.nsIFilePicker.returnReplace){return fp.file;}}catch(e){Components.utils.reportError(e);}return null;},confirm:function(text){var prompts=imns.Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(imns.Ci.nsIPromptService);var check={value:false};var flags=prompts.STD_YES_NO_BUTTONS;var button=prompts.confirmEx(window,"",text,flags,"","","",null,check);return button==0;}};var im_Pref={get psvc(){return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);},get imBranch(){return this.psvc.getBranch("extensions.imacros.");},get defBranch(){return this.psvc.getBranch(null);},_get_branch:function(oldpref){return(oldpref?this.defBranch:this.imBranch);},getIntPref:function(prefName,oldpref){try{return this._get_branch(oldpref).getIntPref(prefName);}catch(e){Components.utils.reportError(e);return null;}},setIntPref:function(prefName,value,oldpref){try{this._get_branch(oldpref).setIntPref(prefName,value);}catch(e){Components.utils.reportError(e);}},getCharPref:function(prefName,oldpref){try{return this._get_branch(oldpref).getCharPref(prefName);}catch(e){Components.utils.reportError(e);return null;}},setCharPref:function(prefName,value,oldpref){try{this._get_branch(oldpref).setCharPref(prefName,value);}catch(e){Components.utils.reportError(e);}},getBoolPref:function(prefName,oldpref){try{return this._get_branch(oldpref).getBoolPref(prefName);}catch(e){Components.utils.reportError(e);return null;}},setBoolPref:function(prefName,value,oldpref){try{this._get_branch(oldpref).setBoolPref(prefName,value);}catch(e){Components.utils.reportError(e);}},getFilePref:function(prefName,oldpref){try{var store=im_Pref.getBoolPref("store-in-profile");if(store&& !oldpref){var ds=imns.Cc["@mozilla.org/file/directory_service;1"].getService(imns.Ci.nsIProperties);var profdir=ds.get("ProfD",imns.Ci.nsILocalFile);profdir.append("iMacros");switch(prefName){case "defdownpath":profdir.append("Downloads");break;case "defdatapath":profdir.append("Datasources");break;case "deflogpath":break;case "defsavepath":profdir.append("Macros");break;default:return this._get_branch(oldpref).getComplexValue(prefName,imns.Ci.nsILocalFile);}return profdir;}else{if(oldpref)return this._get_branch(oldpref).getCharPref(prefName);else return this._get_branch(oldpref).getComplexValue(prefName,imns.Ci.nsILocalFile);}}catch(e){Components.utils.reportError(e);return null;}},setFilePref:function(prefName,value,oldpref){try{this._get_branch(oldpref).setComplexValue(prefName,imns.Ci.nsILocalFile,value);}catch(e){Components.utils.reportError(e);}},getStringPref:function(prefName,oldpref){try{return this._get_branch(oldpref).getComplexValue(prefName,imns.Ci.nsISupportsString).data;}catch(e){Components.utils.reportError(e);return null;}},setStringPref:function(prefName,value,oldpref){try{this._get_branch(oldpref).setComplexValue(prefName,imns.Ci.nsISupportsString,value);}catch(e){Components.utils.reportError(e);}},clearPref:function(prefName,oldpref){try{this._get_branch(oldpref).clearUserPref(prefName);}catch(e){Components.utils.reportError(e);}}};var im_Clipboard={get clip(){return imns.Cc["@mozilla.org/widget/clipboard;1"].getService(imns.Ci.nsIClipboard);},putString:function(txt){var str=imns.Cc["@mozilla.org/supports-string;1"].createInstance(imns.Ci.nsISupportsString);str.data=txt;var trans=imns.Cc["@mozilla.org/widget/transferable;1"].createInstance(imns.Ci.nsITransferable);trans.addDataFlavor("text/unicode");trans.setTransferData("text/unicode",str,txt.length*2);this.clip.setData(trans,null,imns.Ci.nsIClipboard.kGlobalClipboard)},getString:function(){var has_data=this.clip.hasDataMatchingFlavors(["text/unicode"],1,imns.Ci.nsIClipboard.kGlobalClipboard);if(!has_data)return null;var trans=imns.Cc["@mozilla.org/widget/transferable;1"].createInstance(imns.Ci.nsITransferable);trans.addDataFlavor("text/unicode");this.clip.getData(trans,imns.Ci.nsIClipboard.kGlobalClipboard);var str={},len={};trans.getTransferData("text/unicode",str,len);str=str.value.QueryInterface(imns.Ci.nsISupportsString);var txt=str.data.substring(0,len.value/2);return txt;}};function im_msg2con(code,errtext,extract,performance){var s;if(code>0)errtext="Macro compeleted OK.";if(!extract)extract="";if(!performance)performance="";s=errtext+"[iim!E!iim]"+extract+"[iim!S!iim]"+performance;return s;};function __getstr(wnd,id){var s=wnd.document.getElementById(id);return s?s.value:"";};function __int(num){var s=num.toString();s=__strim(s);if(!s.length)return Number.NaN;var n=parseInt(s);if(n.toString().length!=s.length)return Number.NaN;return n;};var __is_windows_int;function __is_windows(){if(typeof(__is_windows_int)=="undefined"){try{var os=Components.classes["@mozilla.org/xre/app-info;1"].getService(Components.interfaces.nsIXULRuntime);__is_windows_int=os.OS.search(/winnt/i)!= -1;}catch(e){Components.utils.reportError(e);__is_windows_int=window.navigator.platform.search(/win/i)!= -1;}}return __is_windows_int;};function __psep(){return __is_windows()?'\\':'/';};function __strim(s){return s.replace(/^\s+/,"").replace(/\s+$/,"");};function __loginf(text){imns.consvc.logStringMessage(text);};function __dump_object(prefix,obj_name,obj){if(!obj){__loginf(prefix+" "+obj_name+" is null");}for(var i in obj){__loginf(prefix+" "+obj_name+"."+i+"="+obj[i]);}}